package com.mojian.controller;

import com.mojian.common.Result;
import com.mojian.service.RecommendationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.dev33.satoken.stp.StpUtil;

import java.util.Map;

/**
 * 个性化推荐控制器
 */
@RestController
@RequestMapping("/protal/personalizedRecommendation")
@RequiredArgsConstructor
@Api(tags = "个性化推荐接口")
@Slf4j
public class PersonalizedRecommendationController {

    private final RecommendationService recommendationService;

    /**
     * 获取个性化推荐文章
     */
    @GetMapping("/personalized")
    @ApiOperation("获取个性化推荐文章")
    public Result<Map<String, Object>> getPersonalizedRecommendations() {
        log.info("获取个性化推荐文章");
        
        // 检查是否已登录，未登录时给出通用推荐
        Long userId = 0L; // 默认为游客
        try {
            if (StpUtil.isLogin()) {
                userId = StpUtil.getLoginIdAsLong();
                log.info("已登录用户[{}]请求个性化推荐", userId);
            } else {
                log.info("未登录用户请求个性化推荐");
            }
        } catch (Exception e) {
            log.warn("获取登录用户ID失败", e);
        }
        
        return Result.success(recommendationService.getPersonalizedRecommendations(userId));
    }

    /**
     * 获取用户浏览数据分析
     */
    @GetMapping("/userAnalysis")
    @ApiOperation("获取用户浏览数据分析")
    public Result<Map<String, Object>> getUserBrowsingAnalysis() {
        log.info("获取用户浏览数据分析");
        
        // 检查是否已登录
        if (!StpUtil.isLogin()) {
            return Result.error(401, "用户未登录");
        }
        
        Long userId = StpUtil.getLoginIdAsLong();
        Map<String, Object> analysisData = recommendationService.getUserBrowsingAnalysis(userId);
        
        return Result.success(analysisData);
    }
} 